<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Controller {

 
	function __construct()
	{
		parent::__construct();
		
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}
		$this->load->model('profile/profile_model');
			$this->load->library('session');
		
		$this->load->library('form_validation');
		$this->load->helper('url');
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :

		$this->load->database();

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
	
	}
		
	function  index(){
	$flag =	$this->profile_model->find_profile();
		if($flag > 0){
			$this->edit_profile();
		}else{
			$this->add_profile();
			}
		}	
	
	function add_profile(){

		$this->data['page_title'] = "Company Profile";
		$meta['page_title'] = "company profile";
		$this->form_validation->set_rules('name', 'Company Name', 'required|xss_clean');
		$this->form_validation->set_rules('tin_no', 'Tin Name', 'required|xss_clean');
		$this->form_validation->set_rules('owner_name', 'Owner Name', 'required|xss_clean');
		$this->form_validation->set_rules('invoice_code', 'Invoice Code', 'required|xss_clean');
		$this->form_validation->set_rules('email', 'Email', 'required|xss_clean');
		$this->form_validation->set_rules('address', 'Address', 'required');
		if ($this->form_validation->run() == true)
		{
			$additional_data = array(
				'name' => strtoupper($this->input->post('name')),
				'tin_no' => $this->input->post('tin_no'),
				'owner_name' => strtoupper($this->input->post('owner_name')),
				'invoice_code' =>strtoupper( $this->input->post('invoice_code')),
				'phone_no' => $this->input->post('phone_no'),
				'address' =>strtoupper( $this->input->post('address')),
	
			);
		}
		if ($this->form_validation->run() == true) 
		{
			$this->profile_model->insert($additional_data);
			redirect("profile/profile/edit_profile");
			
		}
		else
		{
			
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['name'] = array(
				'name'  => 'name',
				'id'    => 'name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('name'),
			);

			
			$this->data['invoice_code'] = array(
				'name'  => 'invoice_code',
				'id'    => 'invoice_code',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('invoice_code'),
			);
			
			
			$this->data['owner_name'] = array(
				'name'  => 'owner_name',
				'id'    => 'owner_name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('owner_name'),
			);

			$this->data['tin_no'] = array(
				'name'  => 'tin_no',
				'id'    => 'tin_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('tin_no'),
			);

           $this->data['email'] = array(
				'name'  => 'email',
				'id'    => 'email',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('email'),
			);
	
			$this->data['phone_no'] = array(
			'name'  => 'phone_no',
			'id'    => 'phone_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone_no'),
			);
	
			$this->data['address'] = array(
			'name'  => 'address',
			'id'    => 'address',
			'type'  => 'textarea',
			'value' => $this->form_validation->set_value('address'),
			);
		
			$this->load->view('common/header', $meta);
			$this->load->view('profile/add_profile', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	function get_all_branch(){
		
	$this->data['users'] = $this->branch_model->get_branch()->result();
	   $meta['page_title'] = "branch List";
	   $this->data['message'] = "branch Added Successfully";
	   $this->load->view('common/header', $meta);
	   $this->load->view('settings/branch/view_branch',$this->data);
       $this->load->view('common/footer');
	       }
	function edit_profile(){
	$this->data['page_title'] = "Company profile";
	$meta['page_title'] = "Company profile";
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth', 'refresh');
		}

		$user = $this->profile_model->edit_profile(1)->row();
		$this->form_validation->set_rules('name', 'Branch Name', 'required|xss_clean');
		$this->form_validation->set_rules('owner_name', 'Owner Name', 'required|xss_clean');
		$this->form_validation->set_rules('invoice_code', 'Invoice Code', 'required|xss_clean');
		$this->form_validation->set_rules('tin_no', 'Tin Number', 'required|xss_clean');
		$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|xss_clean');
		$this->form_validation->set_rules('address', 'Address', 'required|xss_clean');

		if (isset($_POST) && !empty($_POST))
		{
			$data = array(
				'name' => strtoupper($this->input->post('name')),
				'owner_name' =>strtoupper( $this->input->post('owner_name')),
				'invoice_code' => strtoupper($this->input->post('invoice_code')),
				'tin_no' => $this->input->post('tin_no'),				
				'phone_no' => $this->input->post('phone_no'),
				'email' => strtoupper($this->input->post('email')),
				'address' =>strtoupper( $this->input->post('address'))
			);
			if ($this->form_validation->run() === TRUE)
			{
				$this->profile_model->update(1, $data);
				redirect("profile/profile/edit_profile");
			}
		}

		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
		$this->data['user'] = $user;
		$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
		);

			
			$this->data['invoice_code'] = array(
				'name'  => 'invoice_code',
				'id'    => 'invoice_code',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('invoice_code',$user->invoice_code),
			);
			
			
			$this->data['owner_name'] = array(
				'name'  => 'owner_name',
				'id'    => 'owner_name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('owner_name',$user->owner_name),
			);

			$this->data['tin_no'] = array(
				'name'  => 'tin_no',
				'id'    => 'tin_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('tin_no',$user->tin_no),
			);

		$this->data['phone_no'] = array(
			'name'  => 'phone_no',
			'id'    => 'phone_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone_no', $user->phone_no),
		);
		$this->data['email'] = array(
				'name'  => 'email',
				'id'    => 'email',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('invoice_code',$user->email),
			);
			

		$this->data['address'] = array(
			'name'  => 'address',
			'id'    => 'address',
			'type'  => 'textarea',
			'rows'  =>5,
			'value' => $this->form_validation->set_value('address', $user->address),
		);
		
		
		$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Update',
			'class' =>'btn btn-inverse'
		);
		
		$this->data['page_title'] = "Company Profile";
		 $this->load->view('common/header', $meta);
		$this->load->view('profile/edit_profile', $this->data, 1);
		 $this->load->view('common/footer');
	
		
		
	}

}
	
	?>